package com.fastwire.carriere.service;

import java.util.List;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.fastwire.carriere.common.constant.SystemParameterConstants;
import com.fastwire.carriere.common.entity.CodeDTO;
import com.fastwire.carriere.common.exception.ResourceException;
import com.fastwire.carriere.controller.SystemAdminController;
import com.fastwire.carriere.controller.UserController;
import com.fastwire.carriere.controller.impl.SystemAdminControllerImpl;
import com.fastwire.carriere.controller.impl.UserControllerImpl;
import com.fastwire.carriere.dao.CodeDao;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.DAOFactory;
import com.fastwire.carriere.dao.UserSessionDao;
import com.fastwire.carriere.system.vo.SystemParameterVo;
import com.fastwire.carriere.user.vo.UserGroupVo;
import com.fastwire.carriere.user.vo.UserVo;
import com.google.common.collect.Lists;

public class SystemSetUp {

	public void createCodeCateLog() {

		CodeDao codeDao = DAOFactory.getInstance().getCodeDao();

		try {
			List<CodeDTO> codes = codeDao.getCodeDTOsbyCatelog("Permission");
			if (codes != null && !codes.isEmpty()) {
				return;
			}

			List<CodeDTO> codeDTOs = Lists.newArrayList();
			codeDTOs.add(new CodeDTO("eBOD", "", "Permission"));
			codeDTOs.add(new CodeDTO("Parameters", "", "Permission"));
			codeDTOs.add(new CodeDTO("Inventory", "", "Permission"));
			codeDTOs.add(new CodeDTO("UserManagement", "", "Permission"));
			codeDTOs.add(new CodeDTO("Discovery", "", "Permission"));
			codeDTOs.add(new CodeDTO("Report", "", "Permission"));

			DAOFactory.getInstance().getCodeDao().saveOrUpdateAllNetworkEntity(codeDTOs);
		} catch (DAOException e) {
			e.printStackTrace();
			Assert.fail(e.getMessage());
		}

	}

	public void createSystemParameter() {

		List<SystemParameterVo> parameterVos = Lists.newArrayList();

		parameterVos.add(new SystemParameterVo(
				SystemParameterConstants.U2000_PARAMTER_PRIMARYIPADDRESS,
				"129.9.0.5", SystemParameterConstants.CATELOG_U2000));
		parameterVos.add(new SystemParameterVo(
				SystemParameterConstants.U2000_PARAMETER_PORT, "9997",
				SystemParameterConstants.CATELOG_U2000));
		parameterVos.add(new SystemParameterVo(
				SystemParameterConstants.U2000_PARAMTER_SECURITY,
				"admin:ngttu2000", SystemParameterConstants.CATELOG_U2000));

		parameterVos.add(new SystemParameterVo(
				SystemParameterConstants.SYSTEM_PARAMETER_SESSION, "12",
				SystemParameterConstants.CATELOG_SYSTEM));

		try {

			SystemAdminController controller = new SystemAdminControllerImpl();
			for (SystemParameterVo vo : parameterVos) {
				vo.setUpdatedby("root");
				controller.updateSystemParameter(vo);
			}

		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void clearSession() {

		UserSessionDao userSessionDao = DAOFactory.getInstance()
				.getUserSessionDao();

		try {
			userSessionDao.invalidateUserSessions();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void createUserGroup() {

		UserController controller = new UserControllerImpl();

		UserGroupVo userGroup = new UserGroupVo();
		userGroup.setName("Super");
		userGroup.setDescription("Super User Group");

		try {
			controller.createUserGroup(userGroup);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ResourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void createSuperUser() {

		UserController controller = new UserControllerImpl();

		UserVo user = new UserVo("root", "aea6380b5e33432e2f0abda224575d8a",
				"root@singtel.com", "Super");

		try {
			controller.createUser(user);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ResourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void setupAll() {
		createCodeCateLog();
		createSystemParameter();
		createUserGroup();
		createSuperUser();
		// clearSession();
	}
}
